import 'package:flutter/material.dart';
import 'package:flutter_app/controller/MenuController.dart';
import 'package:get/get.dart';

class SlidLeftMenuWidget extends StatelessWidget {
//绑定注入控制器
  final MenuController _menuController = Get.put(MenuController());

  BuildContext get context => context;

  @override
  Widget build(BuildContext context) {
    return Drawer(
      child: Container(
        color: Colors.black,
        child: Obx(() {
          return buildListView();
        }),
      ),
    );
  }

  ListView buildListView() {
    return ListView(
      children: [
        DrawerHeader(
          child: Text(
            "早起的年轻人",
            style: TextStyle(
              fontWeight: FontWeight.w600,
              fontSize: 22,
              color: Colors.red,
            ),
          ),
        ),
        ...List.generate(
          _menuController.menuList.length,
          (index) => SlidMenuItemWidget(
            _menuController.menuList[index],
            _menuController.currentSelectedIndex == index,
            () {
              //变更已选中分类下标
              _menuController.currentSelectedIndex = index;
              //关闭侧拉菜单
              _menuController.homeScaffoldKey.currentState.openEndDrawer();
            },
          ),
        ),
      ],
    );
  }
}

class SlidMenuItemWidget extends StatelessWidget {
  //显示的标题
  final String title;

  //是否选中
  final bool isSelect;

  //当前点击事件
  final Function onTab;

  SlidMenuItemWidget(this.title, this.isSelect, this.onTab);

  @override
  Widget build(BuildContext context) {
    return ListTile(
      onTap: onTab,
      contentPadding: EdgeInsets.only(left: 20),
      selected: isSelect,
      selectedTileColor: Colors.red,
      title: Text(
        title,
        style: TextStyle(
          fontWeight: FontWeight.w600,
          fontSize: 16,
          color: Colors.white,
        ),
      ),
    );
  }
}
